<?php

namespace app\api\controller;

use app\admin\model\Admin;
use app\api\model\CustomerCategory;
use think\facade\Validate;
use think\Request;
use app\api\model\UserModel;
use app\api\model\CustomerCategory as CustomerCategoryModel;

class User extends Common
{
    /**
     * @param Request $request
     * @param UserModel $model
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function index(Request $request, UserModel $model)
    {
        $res = $model->getList($request->param(), $request->param('page', 1), $request->param('size', 10));
        if (!empty($res)) {
            return $this->returnJson(200, '成功', $res);
        } else {
            return $this->returnJson(500, $res);
        }
    }

    /**
     * @param $id
     * @param UserModel $model
     * @return array|\PDOStatement|string|\think\Model|\think\response\Json
     */
    public function show($id, UserModel $model)
    {
        $res = $model->getInfo($id);
        $res->category_name = \app\api\model\CustomerCategory::find($res->category)['name'];
        $res->category_discount = \app\api\model\CustomerCategory::find($res->category)['discount'];
        if (!is_string($res)) {
            return $this->returnJson(200, '成功', $res);
        } else {
            return $this->returnJson(500, $res);
        }
    }

    /**
     * @param Request $request
     * @param UserModel $model
     * @param CustomerCategoryModel $categoryModel
     * @return \think\response\Json
     */
    public function add(Request $request, UserModel $model, CustomerCategoryModel $categoryModel)
    {
        // 获取变量
        $data = $request->only($model->getFieldArray(false));
        $data['area'] = $request->param('area', '');
        // 构建验证规则
        $rule = array_fill_keys(['name','sort',], 'require');
        if (!empty($request->param('category'))) {
            $rule['category'] =  function ($value, $rule) use ($categoryModel) {
                return !is_string($categoryModel->getCategory($value)) ? true : '无效的 category_id';
            };
        }
        // 构建验证器
        $validate = Validate::make($rule);
        if (!$validate->check($data)) {
            return  $this->returnJson(422, $validate->getError());
        }
        $res = $model->getAdd($data);
        if (!empty($res)) {
            return $this->returnJson(200, '成功', $res);
        } else {
            return $this->returnJson(500, '失败');
        }
    }

    /**
     * @param $id
     * @param Request $request
     * @param UserModel $model
     * @param CustomerCategoryModel $categoryModel
     * @return \think\response\Json
     */
    public function edit($id, Request $request, UserModel $model, CustomerCategoryModel $categoryModel)
    {
        // 获取变量
        $data = $request->only($model->getFieldArray(false));
        $data['area'] = $request->param('area', '');
        // 构建验证规则
        $rule = array_fill_keys(['name','sort',], 'require');
        if (!empty($request->param('category'))) {
            $rule['category'] =  function ($value, $rule) use ($categoryModel) {
                return !is_string($categoryModel->getCategory($value)) ? true : '无效的 category_id';
            };
        }
        // 构建验证器
        $validate = Validate::make($rule);
        if (!$validate->check($data)) {
            return  $this->returnJson(422, $validate->getError());
        }
        $res = $model->getEdit($id, $data);
        if (!is_string($res)) {
            return $this->returnJson(200, '成功', $res);
        } else {
            return $this->returnJson(500, $res);
        }
    }

    /**
     * @param $id
     * @param Request $request
     * @param UserModel $model
     * @return \think\response\Json
     */
    public function del($id, Request $request, UserModel $model)
    {
        $res = $model->getDel($id);
        if ($res === true) {
            return $this->returnJson(200, '成功');
        } else {
            return $this->returnJson(500, $res);
        }
    }

    public function getAllMaker(Admin $model)
    {
        $list =$model->getAllMaker();
        if (!empty($list)) {
            return $this->returnJson(200, '成功', $list);
        } else {
            return $this->returnJson(500, '失败');
        }
    }

    public function getAllSales(Admin $model)
    {
        $list =$model->getAllSales();
        if (!empty($list)) {
            return $this->returnJson(200, '成功', $list);
        } else {
            return $this->returnJson(500, '失败');
        }
    }

    public function getAllCustomer($id,Request $request,UserModel $model)
    {
        $list =$model->getAllCustomer($id);
        if (!empty($list)) {
            return $this->returnJson(200, 'ok',$list);
        } else {
            return $this->returnJson(500,'失败');
        }
    }
}
